package cn.yuhh.servlet;

import cn.yuhh.dao.IDinnerTableDao;
import cn.yuhh.entity.DinnerTable;
import cn.yuhh.factory.BeanFactory;
import cn.yuhh.service.IDinnerTableService;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Brill on 2016/12/4.
 */
@WebServlet(name = "DinnerTableServlet", urlPatterns = {"/table"})

public class DinnerTableServlet extends HttpServlet {

    private IDinnerTableService service = BeanFactory.getInstance("dinnerTableService", IDinnerTableService.class);

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        List<DinnerTable> tables = service.query();
        config.getServletContext().setAttribute("tables", tables);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("type/html;charset=utf-8");


        String method = request.getParameter("method");

        System.out.println(request.getParameter("method"));

        if (method != null) {
            if ("add".equals(method)) {
                add(request, response);
            } else if ("list".equals(method)) {
                list(request, response);
            } else if ("up1".equals(method)) {
                update(request, response);
            } else if ("delete".equals(method)) {
                delete(request, response);
            } else if ("search".equals(method)) {
                search(request, response);
            }
        }
    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<DinnerTable> tables = service.query();
        request.setAttribute("tables", tables);
        request.getRequestDispatcher("/sys/board/boardList.jsp").forward(request, response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tableName = request.getParameter("tableName");
        if (tableName != null) {
            DinnerTable table = new DinnerTable();
            table.setTableName(tableName);
            service.add(table);
            list(request, response);
        }
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.delete(Integer.parseInt(id));
        list(request, response);
    }

    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        List<DinnerTable> list = service.query(keyword);
        request.setAttribute("tables", list);
        request.getRequestDispatcher("/sys/board/boardList.jsp");
        list(request, response);
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.changeState(Integer.parseInt(id));
        list(request, response);
    }
}
